package com.yidu.controller;

import com.yidu.entity.SCommodity;
import com.yidu.service.SCommodityService;
import com.yidu.utils.BaseMessage;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * (SCommodity)表控制层
 *
 * @author wanghong
 * @since 2021-07-29 16:30:28
 */
@RestController
@RequestMapping("sCommodity")
public class SCommodityController {
    /**
     * 服务对象
     */
    @Autowired
    private SCommodityService sCommodityService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public SCommodity selectOne(Integer id) {
        return this.sCommodityService.queryById(id);
    }


    /**
     * 分页查询
     * @param sCommodity
     * @return
     */
    @RequestMapping("showList")
    public Map<String,Object> showList(SCommodity sCommodity){
        System.out.println(sCommodity);
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",sCommodityService.findCount(sCommodity));
        map.put("data",sCommodityService.findAll(sCommodity));
        return map;
    }

    /**
     * 删除单个
     * @param coid
     * @return
     */
    @PostMapping("delete")
    public Map<String,Object> delete(Integer coid){
        boolean b = sCommodityService.deleteById(coid);
        if (b){
           return   BaseMessage.judge("1", "删除成功");
        }else {
            return BaseMessage.judge("0","删除失败");
        }
    }

    /**
     * 删除多个
     * @param ids
     * @return
     */
    @PostMapping("deleteBatch")
    public Map<String,Object> deleteBatch(String ids){
        String[] id = ids.split(",");
        boolean b = sCommodityService.deleteBatch(id);
        if (b){
            return   BaseMessage.judge("1", "删除成功");
        }else {
            return BaseMessage.judge("0","删除失败");
        }
    }

    /**
     * 增加或修改
     * @param sCommodity
     * @return
     */
    @PostMapping("addAndUpdate")
    public Map<String,Object> addAndUpdate(
            @RequestParam(value = "coFile",required = false) MultipartFile file,
            SCommodity sCommodity){
        SCommodity sco = null;

        try {
            String name = file.getOriginalFilename();
            if (name!=null&&!name.equals("")) {
                String hz = name.substring(name.lastIndexOf("."));
                String newName = UUID.randomUUID().toString() + hz;
                file.transferTo(new File("I://img/" + newName));
                sCommodity.setCoimg("/upload/" + newName);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (sCommodity.getCoid()!=null){
            sco = sCommodityService.update(sCommodity);
        }else {
            sco = sCommodityService.insert(sCommodity);
        }

        if (sco!=null){
            return BaseMessage.judge("1","成功");
        }else {
            return BaseMessage.judge("0","失败");
        }
    }


    @InitBinder
    public void initBinder(ServletRequestDataBinder binder){
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),
                true));
     }


}
